package net.cloudcodex.client.character.view.panel;

import java.util.Map;

import net.cloudcodex.client.character.view.CharactersView.Message;
import net.cloudcodex.client.util.ClientUtils;
import net.cloudcodex.client.widgets.CharacterAvatar;

import com.google.gwt.event.dom.client.HasKeyDownHandlers;
import com.google.gwt.event.dom.client.HasKeyUpHandlers;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class CharacterSheetInfoPanel extends FlowPanel {

	private boolean isOwner;
	private boolean isGameMaster;
	private Label titleMsg;
	private TextArea publicDescriptionTextArea;
	private TextArea privateDescriptionTextArea;
	private DescriptionPanel publicDescriptionPanel = new DescriptionPanel();
	private DescriptionPanel privateDescriptionPanel = new DescriptionPanel();
	private DisclosurePanel characterSheetAdvancedInfosPanel = new DisclosurePanel();

	private Message message;
	private HorizontalPanel upperDecriptionPanel;

	private String publicDescription;
	private String privateDescription;
	private CharacterAvatar characterAvatar;
	private Map<Long, String> aliases;
	private Long ownerId;
	private final CharacterAdvancedInfosPanel advancedInfosPanel = new CharacterAdvancedInfosPanel();

	public CharacterSheetInfoPanel() {
		this(false, false);
	}

	public CharacterSheetInfoPanel(boolean isOwner, boolean isGameMaster) {
		// this.characterCDO = characterCTO;
		this.isOwner = isOwner;
		this.isGameMaster = isGameMaster;

	}

	public void initComponents() {
		clear();
		upperDecriptionPanel = new HorizontalPanel();
		if (isGameMaster && aliases != null) {
			String alias = aliases.get(null);
			if (alias != null) {
				characterAvatar.initAliasPanel(alias);
			}
		}
		characterAvatar.removeAvatarStyleName();
		upperDecriptionPanel.add(characterAvatar);
		publicDescriptionPanel.initDescriptiontextArea();
		titleMsg = new Label(message.getPublicDescriptionTitle());
		publicDescriptionPanel.add(titleMsg);
		initPublicDescriptiontextArea();
		publicDescriptionPanel.add(publicDescriptionTextArea);

		upperDecriptionPanel.add(publicDescriptionPanel);
		this.add(upperDecriptionPanel);
		Label advancedInfosTitleLabel = new Label(
				message.getAdvancedInfosTitle());
		advancedInfosTitleLabel.addStyleName("titlePanel");
		characterSheetAdvancedInfosPanel.setHeader(advancedInfosTitleLabel);
		characterSheetAdvancedInfosPanel.setWidth("100%");
		characterSheetAdvancedInfosPanel.setAnimationEnabled(true);
		advancedInfosPanel.setMessage(message);

		advancedInfosPanel.initComponents();

		// characterSheetAdvancedInfosPanel.setContent(privateDescriptionPanel);
		if (isGameMaster) {

			characterSheetAdvancedInfosPanel.setContent(advancedInfosPanel);

			this.add(characterSheetAdvancedInfosPanel);
		}

		if ((isOwner || isGameMaster)) {

			privateDescriptionPanel.initDescriptiontextArea();
			titleMsg = new Label(message.getPrivateDescriptionTitle());
			privateDescriptionPanel.add(titleMsg);
			initPrivateDescriptiontextArea();
			privateDescriptionPanel.add(privateDescriptionTextArea);
			if (isOwner || ownerId != null) {
				this.add(privateDescriptionPanel);
			}
		}
		initStyleName();

	}

	private void initPublicDescriptiontextArea() {
		this.publicDescriptionTextArea = publicDescriptionPanel
				.getDescriptionTextArea();
		publicDescriptionTextArea.setEnabled(isGameMaster);
		String descriptionText = publicDescription;
		publicDescriptionTextArea.setText(descriptionText);
		publicDescriptionTextArea.setTitle("publicDescription");
	}

	private void initPrivateDescriptiontextArea() {
		this.privateDescriptionTextArea = privateDescriptionPanel
				.getDescriptionTextArea();
		privateDescriptionTextArea.setEnabled(isGameMaster);
		String descriptionText = privateDescription;
		privateDescriptionTextArea.setText(descriptionText);
		privateDescriptionTextArea.setTitle("privateDescription");

	}

	private void initStyleName() {
		titleMsg.setStyleName("titlePanel");
		if (privateDescriptionTextArea != null){
			privateDescriptionTextArea.addStyleName("characterDescriptionPanel");
			privateDescriptionTextArea.addStyleName(
					"privateCharacterDescriptionPanel");
		}
		publicDescriptionTextArea.setStyleName("characterDescriptionPanel",
				true);
		publicDescriptionTextArea.setStyleName(
				"publicCharacterDescriptionPanel", true);

	}

	public void setOwner(boolean isOwner) {
		this.isOwner = isOwner;
	}

	public void setGameMaster(boolean isGameMaster) {
		this.isGameMaster = isGameMaster;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public String getChangedPublicDescription() {
		String newText = publicDescriptionTextArea.getText();
		String publicDescritpion = publicDescription;
		return ClientUtils.getChangedText(newText, publicDescritpion);
	}

	public String getChangedPrivateDescription() {
		String newText = "";
		if (privateDescriptionTextArea != null){
			newText = privateDescriptionTextArea.getText();
		}
		String privateDescritpion = privateDescription;
		return ClientUtils.getChangedText(newText, privateDescritpion);
	}

	public void refreshPublicDescrption(String description) {
		if (description != null) {
			publicDescription = description;
			publicDescriptionTextArea.setText(description);
		}
	}

	public void refreshPrivateDescrption(String description) {
		if (description != null) {
			privateDescription = description;
			privateDescriptionTextArea.setText(description);
		}
	}

	public void addCharacterPublicDescription(String description) {
		this.publicDescription = description;
	}

	public void addCharacterPrivateDescription(String description) {
		this.privateDescription = description;
	}

	public void addCharacter(long id, String icon, String name) {
		CharacterAvatar characterAvatar = new CharacterAvatar(icon, name);
		characterAvatar.setId(id);
		this.characterAvatar = characterAvatar;
		this.advancedInfosPanel.setName(name);
		this.advancedInfosPanel.setIcon(icon);

	}

	public void addAliases(Map<Long, String> aliases) {
		this.aliases = aliases;

	}

	public HasKeyDownHandlers getPrivateDescriptionPanelKeyDownHandlers() {
		return privateDescriptionPanel;
	}

	public HasKeyUpHandlers getPrivateDescriptionPanelKeyUpHandlers() {
		return privateDescriptionPanel;
	}

	public HasKeyDownHandlers getPublicDescriptionPanelKeyDownHandlers() {
		return publicDescriptionPanel;
	}

	public HasKeyUpHandlers getPublicDescriptionPanelKeyUpHandlers() {
		return publicDescriptionPanel;
	}

	public Map<Long, String> getAliases() {
		return this.aliases;
	}

	public void setOwnerId(Long ownerId) {
		this.ownerId = ownerId;
	}

	public void addCharacterAdvancedInfos(boolean isProfile, boolean isLocked,
			boolean isDead) {
		advancedInfosPanel.setProfile(isProfile);
		advancedInfosPanel.setLocked(isLocked);
		advancedInfosPanel.setDead(isDead);
	}

	public void isActive(boolean isActive) {
		advancedInfosPanel.isActive(isActive);
	}

	public TextBox getNameTextBox() {
		return advancedInfosPanel.getNameTextBox();
	}

	public TextBox getIconTextBox() {
		return advancedInfosPanel.getIconTextBox();
	}

	public HasValueChangeHandlers<Boolean> getProfileCheckBox() {
		return advancedInfosPanel.getProfileChkBx();
	}

	public HasValueChangeHandlers<Boolean> getLockedCheckBox() {
		return advancedInfosPanel.getLockedChkBx();
	}

	public HasValueChangeHandlers<Boolean> getDeadCheckBox() {
		return advancedInfosPanel.getDeadChkBx();
	}

	public Boolean isDead() {
		return advancedInfosPanel.isDead();
	}

	public Boolean isLocked() {
		return advancedInfosPanel.isLocked();
	}

	public Boolean isProfile() {
		return advancedInfosPanel.isProfile();
	}

}

class DescriptionPanel extends FlowPanel implements HasKeyDownHandlers,
		HasKeyUpHandlers {
	TextArea descriptionTextArea = new TextArea();

	public void initDescriptiontextArea() {
		descriptionTextArea.setCharacterWidth(60);
		descriptionTextArea.setVisibleLines(5);
	}

	public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) {
		return descriptionTextArea.addKeyUpHandler(handler);
	}

	public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
		return descriptionTextArea.addKeyDownHandler(handler);
	}

	public TextArea getDescriptionTextArea() {
		return descriptionTextArea;
	}

}
